﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QFlow.Data;
using QFlow.Core;

namespace QFlow.Windows.Forms
{
    public partial class AccountingLog : Utils.BaseForm
    {
        public AccountingLog(QFlowDbContext context)
            : base(context)
        {
            InitializeComponent();
        }

        private void btnChangeUser_Click(object sender, EventArgs e)
        {
            Login login = new Login(this.Context, true);
            if (login.ShowDialog() == DialogResult.OK)
            {
                foreach (var form in this.MdiParent.MdiChildren)
                {
                    if (form != this)
                        form.Close();
                }
                this.Close();
            }
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(QFlow.Windows.Properties.Resources.MBResetValues, QFlow.Windows.Properties.Resources.MBConfirmCaption, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                AccountingManager.ResetCredit();
                RefreshForm();
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            NewPayment frm = new NewPayment(Context, true);
            if (frm.ShowDialog() == DialogResult.OK)
            {
                AccountingManager.AddPayment(frm.Amount, frm.SelectedAccount);
                RefreshForm();
            }
        }

        private void btnDraw_Click(object sender, EventArgs e)
        {
            NewPayment frm = new NewPayment(Context, false);
            if (frm.ShowDialog() == DialogResult.OK)
            {
                AccountingManager.WithdrawPayment(frm.Amount, frm.SelectedAccount);
                RefreshForm();
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void AccountingLog_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();
            e.Cancel = true;
        }

        private void AccountingLog_Activated(object sender, EventArgs e)
        {
            RefreshForm();
        }

        private void RefreshForm()
        {
            txtCashir.Text = Context.Accounts.GetCashirAccount().Amount.ToString("F");
            txtSafe.Text = Context.Accounts.GetSafeAccount().Amount.ToString("F");
            txtPCredit.Text = Accounting.PurchaseCredit.ToString("F");
            txtPReturn.Text = Accounting.PurchaseReturn.ToString("F");
            txtSCredit.Text = Accounting.SalesCredit.ToString("F");
            txtSReturn.Text = Accounting.SalesReturn.ToString("F");
            txtUser.Text = Authentication.CurrentUser.DisplayName;
        }
    }
}
